#include"tcpclient.h"
#include "globalvariables.h"
#include "qdatetime.h"
#include<QThread>
#include<QTcpSocket>
#include<QHostAddress>
#include<QNetworkProxy>
#include<QNetworkInterface>

#define MYLOG qDebug() << "[" << __FILE__ << ":" << __LINE__ << ":" << QThread::currentThread() << __func__ << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")<< "]"
#define MYLOGWarn qWarning() << "[" << __FILE__ << ":" << __LINE__ << ":" << QThread::currentThread() << __func__ << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")<< "]"
TcpClient::TcpClient(QTcpSocket* socket)
{
  m_socket = socket;

}

TcpClient::~TcpClient()
{
  if(m_socket != nullptr)
    {
      delete m_socket;
    }
  m_socket = nullptr;
}

void TcpClient::readMessage()
{

  if (m_socket == nullptr) {
      qWarning() << "Socket is not set!";
      return;
    }

  try{
    if(m_socket->state() == QAbstractSocket::ConnectedState)
      {
        QByteArray  recvMsg = m_socket->readAll();
        MYLOG<<recvMsg;
        if (recvMsg.size()>0){
            QByteArray temp;
            for(int i = 0;i < recvMsg.size();i++)
              {
                if(recvMsg.at(i) == '\n')
                  {
//                    d_queue.enqueue(temp);

                    emit signalIsforclientInfo(temp);
                    qDebug()<<"temp :  "<<temp;

                    temp.clear();
                  }else{
                    temp.append(recvMsg.at(i));
                  }
              }
          }else{

            return ;
          }
      }
  }
  catch(std::exception &ex)
  {
    qWarning()<<"InWork:readMessage"<<ex.what();
    return ;
  }
}






